//
//  LibColCell.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/11.
//

import UIKit

class LibColCell: UICollectionViewCell {
    
    lazy var bookImage: UIImageView = {
        let image = UIImageView()
        image.contentMode = .scaleAspectFill
        image.wn_cornerWithBorder(2)
        image.clipsToBounds = true
        return image
    }()
    
    lazy var addImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "library_add"))
        image.isHidden = true
        return image
    }()
    
    lazy var disLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 9)
        label.textColor = .white
        label.backgroundColor = .init(named: "red_FE95B4")
        label.textAlignment = .center
        label.isHidden = true
        return label
    }()
    
    lazy var selBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "setting_feedback_chose_white"), for: .normal)
        btn.setImage(UIImage(named: "library_book_sel"), for: .selected)
        btn.isHidden = true
        return btn
    }()

    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 11)
        label.textColor = .black
        label.numberOfLines = 2
        return label
    }()

    var isEdit: Bool? {
        didSet {
            selBtn.isHidden = !(isEdit ?? false)
        }
    }
    
    var isBookDetail = false
    
    var model: BookDetailModel? {
        didSet {
            if let m = model {
                bookImage.backgroundColor = .white
                addImage.isHidden = true
                bookImage.wn_setupImg(m.coverUrl, placeHolder: "placeholder_cover_ver")
                nameLabel.text = m.name
                if isBookDetail {
                }else {
                    selBtn.isSelected = m.isSel
                }
            }else {
                bookImage.backgroundColor = .init(named: "white_F7F7F7")
                bookImage.image = UIImage(named: "")
                addImage.isHidden = false
                nameLabel.text = ""
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(bookImage)
        bookImage.addSubview(disLabel)
        bookImage.addSubview(selBtn)
        bookImage.addSubview(addImage)
        addSubview(nameLabel)
        bookImage.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(0)
            make.size.equalTo(CGSize(width: scaleWidth(100), height: scaleWidth(144)))
        }
        disLabel.snp.makeConstraints { (make) in
            make.top.equalTo(5)
            make.right.equalTo(-5)
            make.height.equalTo(14)
            make.width.equalTo(0)
        }
        selBtn.snp.makeConstraints { (make) in
            make.bottom.equalTo(-6)
            make.right.equalTo(-6)
            make.width.height.equalTo(22)
        }
        addImage.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(bookImage.snp.left)
            make.right.equalTo(bookImage.snp.right)
            make.top.equalTo(bookImage.snp.bottom).offset(4)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
